//
//  HCParentMenuViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCParentMenuViewController.h"

#import "HCLogViewController.h"

@interface HCParentMenuViewController ()


@end

@implementation HCParentMenuViewController
@synthesize textTitle = _textTitle;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    self.dataRequest = [[NetConnectionDataRequester alloc] init];
    self.dataRequest.delegate = self;
    
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect makeRect = CGRectMake(0, 0, YBKWinFrame.size.width, YBKWinFrame.size.height - YHTabBarHeight);;
    self.view.frame = makeRect;
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, makeRect.size.width, YHKHEADERHEIGHT)];
//    headerView.backgroundColor = YBKRGBCOLOR(240, 96, 96, 1);
    self.headerView.backgroundColor = YBKRGBCOLOR(255, 187, 0, 1);
    [self.view addSubview:self.headerView];
    
    CGFloat ww = YBKWinFrame.size.height - TabBarHeight;
    makeRect = CGRectMake(0, YHKHEADERHEIGHT, YHGetSizeWidth(self.view), ww - (YHKHEADERHEIGHT));
    self.centerView  = [[UIView alloc] initWithFrame:makeRect];
    self.centerView.backgroundColor = YBKHEXCOLOR(BgColorValue);
    [self.centerView setClipsToBounds:YES];
    [self.view addSubview:self.centerView];
    
    self.centerView__ = self.centerView;
    
    [self initHeaderView];
    
    // Do any additional setup after loading the view.
    NSLog(@"当前屏幕宽度:%@",NSStringFromCGSize([UIScreen mainScreen].currentMode.size));
    NSLog(@"屏幕宽度：%@",NSStringFromCGSize([UIScreen mainScreen].bounds.size));
}

#pragma mark -- 网络请求回调
-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {
    NSLog(@"成功");
}

-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    NSLog(@"失败");
}

#pragma mark -- 倒计时按钮
-(void)theWayLessTimeButton:(UIButton *)button WithTime:(NSInteger)time AndTitle:(NSString *)title{
    //    UIButton *buttonLess = (UIButton *)button;
    button.titleLabel.numberOfLines = 0;
    __block int timeout= (int)time;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 创建一个定时器(dispatch_source_t本质还是个OC对象,创建出来的对象需要强引用)
    
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置定时器的各种属性(几时开始任务，每隔多长时间执行一次)
    // GCD的时间参数，一般是纳秒(1秒 = 10的9次方纳秒)
    // 何时开始执行第一个任务
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)); //比当前时间晚1秒
    //    dispatch_time_t start = DISPATCH_TIME_NOW;// 当前时间执行
    uint64_t interval= 2.0 * NSEC_PER_SEC;
    dispatch_source_set_timer(self.timer, start, interval, 0); // NSEC_PER_SEC 纳秒
    // 设置回调
    dispatch_source_set_event_handler(self.timer, ^{
        
        timeout--;
        //        记好了刷新UI的时候一定要在主队列中刷新,就是主线程.
        dispatch_async(dispatch_get_main_queue(), ^{
            button.userInteractionEnabled=NO;
            [button setTitle:[NSString stringWithFormat:@"   %ds后重新获取   ",timeout] forState:UIControlStateNormal];
            //            NSLog(@"-----%d",timeout);
            if (timeout == 1) {
                
                [button setTitle:@"   获取验证码   " forState:UIControlStateNormal];
                button.backgroundColor = YBKRGBCOLOR(219, 112, 33, 1);
                button.userInteractionEnabled=YES;
                dispatch_cancel(self.timer);
                self.timer = nil;
            }
            
        });
        /*
         
         GCD的定时器要比NStimer的定时器要精确的多.
         
         
         
         
         */
        
    });
    // 启动定时器
    dispatch_resume(self.timer);
    
}


#pragma mark - 初始化HeaderView
-(void)initHeaderView{
    CGRect makeRect = YBKWinFrame;
    CGFloat leftWidth=55.0f;
    CGFloat rightWidth=55.0f;
    CGFloat offsetX=5.0f;//距离左边的偏移量
    CGFloat contengHeight=44.0f;//不管ios7还是以下头部高度都是44.0
    self.leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.leftButton.frame = CGRectMake(offsetX, IOS7 ? 20.0f: 0, leftWidth, contengHeight);
    self.leftButton.backgroundColor = [UIColor clearColor];
    
    [self.leftButton setBackgroundImage:nil forState:UIControlStateNormal];
    [self.leftButton setBackgroundImage:nil forState:UIControlStateHighlighted];
    self.leftButton.tag = 0;
    [self.leftButton addTarget:self action:@selector(mainBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.headerView addSubview:self.leftButton];
    
    self.leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.leftButton.frame.size.width -30.0f)/2-8.0f, (self.leftButton.frame.size.height-22.0f)/2, 22.0f, 22.0f)];
    self.leftImageView.image = [UIImage imageNamed:@"icon_back.png"];
    self.leftImageView.contentMode=UIViewContentModeScaleAspectFit;
    [self.leftButton addSubview:self.leftImageView];
    
    self.leftLabel  = [[WZLabel alloc] initWithString:nil fontSize:YHKFONTSIZE_4 alignment:NSTextAlignmentCenter Frame:CGRectMake(0,self.leftButton.frame.size.height-20.0f, self.leftButton.frame.size.width, 20.0f)];
    self.leftLabel.font = YBKSystemFont(YHKFONTSIZE_4);
    self.leftLabel.textColor  = YBKHEXCOLOR(@"#ffffff");
    [self.leftLabel setVerticalAlignment:WZVerticalAlignmentMiddle];
    [self.leftButton addSubview:self.leftLabel];
    
    
    self.headerLabel = [[WZLabel alloc] initWithString:_textTitle fontSize:YHKFONTSIZE_5 alignment:NSTextAlignmentCenter Frame:CGRectMake(leftWidth+offsetX,  IOS7 ? 30.0f: 0, YHGetScreenWidth-(leftWidth+offsetX)*2, contengHeight)];
    self.headerLabel.font = YBKSystemFont(YHKFONTSIZE_6);
    self.headerLabel.textColor  = [UIColor blackColor];
    self.headerLabel.tag  = 188788;
    [self.headerLabel setVerticalAlignment:WZVerticalAlignmentMiddle];
    self.headerLabel.adjustsFontSizeToFitWidth = YES;
    [self.headerView addSubview:self.headerLabel];
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.rightButton.frame = CGRectMake(makeRect.size.width - rightWidth-offsetX, IOS7 ? 20.0f: 0, rightWidth, contengHeight);
    [self.rightButton setBackgroundImage:nil forState:UIControlStateNormal];
    [self.rightButton setBackgroundImage:nil forState:UIControlStateHighlighted];
    self.rightButton.tag = 1;
    [self.rightButton addTarget:self action:@selector(mainBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.headerView addSubview:self.rightButton];
    
    self.rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.rightButton.frame.size.width - 42.0f)/2-8.0f, (self.rightButton.frame.size.height-42.0f)/2, 42.0f, 42.0f)];
    [self.rightButton addSubview:self.rightImageView];
    
    self.rightLabel  = [[WZLabel alloc] initWithString:nil fontSize:12.0f alignment:NSTextAlignmentCenter Frame:CGRectMake(0,self.rightButton.frame.size.height-28.0f, self.rightButton.frame.size.width, 20.0f)];
    self.rightLabel.adjustsFontSizeToFitWidth = YES;
    self.rightLabel.font = YBKSystemFont(YHKFONTSIZE_5);
    self.rightLabel.textColor  = [UIColor whiteColor];
    [self.rightLabel setVerticalAlignment:WZVerticalAlignmentMiddle];
    [self.rightButton addSubview:self.rightLabel];
    
    [self setLeftClear];
    [self setRightClear];
}

-(void)mainBtnAction:(UIButton *)mainBtn {
    if (mainBtn.tag == 0) {
        [self.navigationController popViewControllerAnimated:YBKAnimation];
    }else if (mainBtn.tag == 1) {
        [self rightBtnAction:mainBtn];
    }
}

-(void)rightBtnAction:(UIButton *)btn {
    NSLog(@"右按钮点击");
}

-(void)setTextTitle:(NSString *)textTitle {
    _textTitle = textTitle;
    self.headerLabel.text = _textTitle;
}

-(NSString *)textTitle {
    return self.textTitle_;
}

-(void)setLeftClear {
    self.leftButton.userInteractionEnabled = NO;
    self.leftLabel.hidden = YES;
    self.leftImageView.hidden = YES;
}

-(void)setLeftButtonText:(NSString *)text {
    self.leftButton.userInteractionEnabled = YES;
    self.leftImageView.hidden = YES;
    //设置标题
    self.leftLabel.hidden = NO;
    self.leftLabel.text = text;
}

-(void)setLeftButtonImage:(NSString *)imageName {
    self.leftButton.userInteractionEnabled = YES;
    self.leftLabel.hidden = YES;
    self.leftImageView.hidden = NO;
    if(imageName){
        self.leftImageView.image = [UIImage imageNamed:imageName];
    }
}

-(void)setRightButtonText:(NSString *)text {
    self.rightButton.userInteractionEnabled = YES;
    self.rightImageView.hidden = YES;
    self.rightLabel.hidden = NO;
    self.rightLabel.font = [UIFont systemFontOfSize:14];
    self.rightLabel.text = text;
}

-(void)setRightButtonImage:(NSString *)imageName {
    self.rightButton.userInteractionEnabled = YES;
    self.rightLabel.hidden = YES;
    self.rightImageView.hidden = NO;
    if(imageName){
        UIImage *image = [UIImage imageNamed:imageName];
        CGFloat imageWidth = image.size.width/2.0f;
        CGFloat imageHeight = image.size.height/2.0f;
        self.rightImageView.frame  = CGRectMake((YHGetSizeWidth(self.rightButton)-imageWidth)/2.0f+5.0f,YHGetSizeHeight(self.rightButton)-imageHeight-8.0f, imageWidth, imageHeight);
        self.rightImageView.image = image;
    }
}

-(void)setRightClear {
    self.rightButton.userInteractionEnabled = NO;
    self.rightLabel.hidden = YES;
    self.rightImageView.hidden = YES;
}

-(void)pushChildrenViewController:(NSString *)name parameterObject:(id)type dataObject:(id)dObject {
    id controller = [[NSClassFromString(name) alloc] init];
    if (controller) {
        if (type) {
            ((HCParentMenuViewController *)controller).parameterObject = type;
        }
        if (dObject) {
            ((HCParentMenuViewController *)controller).dataObject = dObject;
        }
        [YBKShareAppDelegate.navigationController pushViewController:controller animated:YBKAnimation];
    }
}

-(void)dealloc{
    self.headerView = nil;
    self.centerView = nil;
    
    self.leftButton = nil;
    self.leftImageView = nil;
    self.leftLabel = nil;
    
    self.rightButton = nil;
    self.rightImageView = nil;
    self.rightLabel = nil;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if (!self.isSelectedVC) {
        self.view = nil;
    }
    
    // Dispose of any resources that can be recreated.
}

- (UIStatusBarStyle)preferredStatusBarStyle {

    return UIStatusBarStyleLightContent;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
